/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-18 09:39:30
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-21 15:27:47
 * @FilePath: \ems_neutral_app\lib\pages\Mine\MinePage\MineController.dart
 * @Description: 个人中心控制器
 */
import 'package:ems_neutral_app/bloc/userbloc/user_bloc.dart';
import 'package:ems_neutral_app/entities/CompanyEntity.dart';
import 'package:ems_neutral_app/entities/UserEntity.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/toast/SilToastExtends.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_utils_package/sil_utils_package.dart';

import '../../../api/ApiUtils.dart';
import '../../../api/request/CommonRequest.dart';

class MineController extends GetxController {
  Rx<CompanyEntity> companyEntity = CompanyEntity().obs;
  Rx<UserEntity?> userEntity = (UserBlocTool.userEntity).obs;

  /// 获取公司相关信息
  getCompanyInfo(
      {required BuildContext context, Function(dynamic)? successHandler}) {
    apiShow(context, api: api_company_info(), success: (res) {
      if (successHandler != null) successHandler(res);
      this.companyEntity.value = res.data;
    }, showLoading: false);
  }

  /// 商务合作
  requestCompanyIcooperate(
      {required BuildContext context,
      String? name,
      String? countryId,
      String? phone,
      String? areaCode,
      String? email,
      String? cooperateContent,
      Function(dynamic)? successHandler}) {
    /// 验证
    /// 姓名
    if (SilTools.stringIsEmpty(name)) {
      SilToastExtends.toastMsg(LangOf(context)?.common?.name_notEmpty);
      return;
    }

    /// 国家
    if (SilTools.stringIsEmpty(countryId)) {
      SilToastExtends.toastMsg(LangOf(context)?.common?.country_notEmpty);
      return;
    }

    /// 手机号
    if (!SilTools.stringIsEmpty(phone)) {
      if (!phone!.phoneNumber()) {
        SilToastExtends.toastMsg(LangOf(context)?.common?.phone_correctFormat);
        return;
      }
    }

    /// 邮箱
    if (SilTools.stringIsEmpty(email)) {
      SilToastExtends.toastMsg(LangOf(context)?.common?.email_notEmpty);
      return;
    } else {
      if (!email!.email()) {
        SilToastExtends.toastMsg(LangOf(context)?.common?.email_correctFormat);
        return;
      }
    }

    /// 详细内容
    if (SilTools.stringIsEmpty(cooperateContent)) {
      SilToastExtends.toastMsg(LangOf(context)?.common?.cooperation_notEmpty);
      return;
    }

    apiShow(context,
        api: api_company_cooperate(
            cooperateContent: cooperateContent!,
            countryId: countryId!,
            email: email,
            name: name!,
            phone: phone,
            areaCode: areaCode), success: (res) {
      if (successHandler != null) successHandler(res);
      SilToastExtends.toastMsg(LangOf(context)?.common?.submit_success);
    }, showLoading: false);
  }

  /// 请求意见反馈
  requestFeedback(
      {required BuildContext context,
      String? content,
      String? contact_way,
      List<String>? images,
      Function(dynamic)? successHandler}) {
    if (SilTools.stringIsEmpty(content)) {
      SilToastExtends.toastMsg(LangOf(context)?.common?.option_notEmpty);
      return;
    }

    apiShow(context,
        api: api_common_feedback(
            content: content!,
            contact_way: contact_way,
            images: images), success: (res) {
      if (successHandler != null) successHandler(res);
      SilToastExtends.toastMsg(LangOf(context)?.common?.submit_success);
    }, showLoading: false);
  }
}
